package models

import (
	"strconv"

	orm "go-papers/database"
)

type Post struct {
	PostId    int    `gorm:"primary_key;AUTO_INCREMENT" json:"postId"` //岗位编号
	DeptId    int    `json:"deptId" gorm:"type:int(11);"`              //部门编码
	DeptName  string `json:"deptName"  gorm:"type:varchar(128);"`      //部门名称
	PostName  string `gorm:"type:varchar(128);" json:"postName"`       //岗位名称
	PostCode  string `gorm:"type:varchar(128);" json:"postCode"`       //岗位代码
	PostNums  int    `gorm:"type:int(11);" json:"postNums"`            //岗位代码
	Sort      int    `gorm:"type:int(4);" json:"sort"`                 //岗位排序
	Status    string `gorm:"type:int(1);" json:"status"`               //状态
	Remark    string `gorm:"type:varchar(255);" json:"remark"`         //描述
	CreateBy  string `gorm:"type:varchar(128);" json:"createBy"`
	UpdateBy  string `gorm:"type:varchar(128);" json:"updateBy"`
	DataScope string `gorm:"-" json:"dataScope"`
	Params    string `gorm:"-" json:"params"`
	BaseModel
}

func (Post) TableName() string {
	return "sys_post"
}

func (e *Post) Create() (Post, error) {
	var doc Post
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e

	var role SysRole
	role.RoleId = doc.PostId
	role.RoleKey = strconv.Itoa(doc.PostId) + "_" + doc.PostCode
	role.RoleName = doc.PostName
	role.RoleSort = 1
	role.Status = "0"
	role.Remark = doc.Remark
	orm.Eloquent.Table("sys_role").Create(&role)

	return doc, nil
}

func (e *Post) Get() (Post, error) {
	var doc Post

	table := orm.Eloquent.Select("sys_post.*,sys_dept.dept_name").Table(e.TableName())
	table = table.Joins("left join sys_dept on sys_dept.dept_id = sys_post.dept_id")
	if e.DeptName != "" {
		table = table.Where("sys_dept.dept_name like ?", "%"+e.DeptName+"%")
	}
	if e.PostId != 0 {
		table = table.Where("post_id = ?", e.PostId)
	}
	if e.PostName != "" {
		table = table.Where("post_name = ?", e.PostName)
	}
	if e.PostCode != "" {
		table = table.Where("post_code = ?", e.PostCode)
	}
	if e.Status != "" {
		table = table.Where("status = ?", e.Status)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *Post) GetList(deptId int) ([]Post, error) {
	var doc []Post

	table := orm.Eloquent.Select("sys_post.*,sys_dept.dept_name").Table(e.TableName())
	table = table.Joins("left join sys_dept on sys_dept.dept_id = sys_post.dept_id")
	if deptId > 0 {
		table = table.Where("sys_post.dept_id = ?", deptId)
	}
	if e.DeptName != "" {
		table = table.Where("sys_dept.dept_name like ?", "%"+e.DeptName+"%")
	}
	if e.PostId != 0 {
		table = table.Where("post_id = ?", e.PostId)
	}
	if e.PostName != "" {
		table = table.Where("post_name = ?", e.PostName)
	}
	if e.PostCode != "" {
		table = table.Where("post_code = ?", e.PostCode)
	}
	if e.Status != "" {
		table = table.Where("status = ?", e.Status)
	}

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "sys_dept", e.DataScope, "职位")

	if err := table.Find(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *Post) GetPage(pageSize int, pageIndex int) ([]Post, int, error) {
	var doc []Post

	table := orm.Eloquent.Select("sys_post.*,sys_dept.dept_name").Table(e.TableName())
	table = table.Joins("left join sys_dept on sys_dept.dept_id = sys_post.dept_id")
	if e.DeptName != "" {
		table = table.Where("`sys_dept`.dept_name like ?", "%"+e.DeptName+"%")
	}
	if e.PostId != 0 {
		table = table.Where("`sys_post`.post_id = ?", e.PostId)
	}
	if e.DeptId != 0 {
		table = table.Where("`sys_post`.dept_id = ?", e.DeptId)
	}
	if e.PostName != "" {
		table = table.Where("`sys_post`.post_name like ?", "%"+e.PostName+"%")
	}
	if e.Status != "" {
		table = table.Where("`sys_post`.status = ?", e.Status)
	}

	table, _ = HandleOrganCommonTable(table, "sys_dept", e.DataScope, "部门职位列表")

	var count int

	if err := table.Order("sort").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`sys_post`.`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

func (e *Post) Update(id int) (update Post, err error) {
	if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}

	var role SysRole
	role.RoleId = e.PostId
	role.RoleKey = e.PostCode
	role.RoleName = e.PostName
	role.RoleSort = 1
	orm.Eloquent.Table("sys_role").Where("role_id = ?", e.PostId).Update(&role)

	return
}

func (e *Post) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("post_id = ?", id).Delete(&Post{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

func (e *Post) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("post_id in (?)", id).Delete(&Post{}).Error; err != nil {
		return
	}

	if err = orm.Eloquent.Table("sys_role").Where("role_id in (?)", id).Delete(&SysRole{}).Error; err != nil {
		return
	}

	Result = true
	return
}
